/*
	Adobe Systems Incorporated(r) Source Code License Agreement
	Copyright(c) 2007 Adobe Systems Incorporated. All rights reserved.
	
	Please read this Source Code License Agreement carefully before using
	the source code.
	
	Adobe Systems Incorporated grants to you a perpetual, worldwide, non-exclusive, 
	no-charge, royalty-free, irrevocable copyright license, to reproduce,
	prepare derivative works of, publicly display, publicly perform, and
	distribute this source code and such derivative works in source or 
	object code form without any attribution requirements.  
	
	The name "Adobe Systems Incorporated" must not be used to endorse or promote products
	derived from the source code without prior written permission.
	
	You agree to indemnify, hold harmless and defend Adobe Systems Incorporated from and
	against any loss, damage, claims or lawsuits, including attorney's 
	fees that arise or result from your use or distribution of the source 
	code.
	
	THIS SOURCE CODE IS PROVIDED "AS IS" AND "WITH ALL FAULTS", WITHOUT 
	ANY TECHNICAL SUPPORT OR ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
	BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
	FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  ALSO, THERE IS NO WARRANTY OF 
	NON-INFRINGEMENT, TITLE OR QUIET ENJOYMENT.  IN NO EVENT SHALL ADOBE
	OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
	EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
	PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
	OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
	WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
	OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOURCE CODE, EVEN IF
	ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package controls.visualizers
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.utils.ByteArray;
	import flash.display.BitmapData;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	import flash.geom.ColorTransform;
	import flash.media.SoundMixer;
	import flash.events.MouseEvent;
	import mx.core.UIComponent;
	import mx.events.FlexEvent;
	import mx.events.StateChangeEvent;
	import flash.display.Bitmap;
	import flash.display.Shape;
	import mx.effects.easing.Back;
	import mx.containers.Canvas;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFieldAutoSize;
	import controls.visualizers.IVisualizer;
	
	public class AMSpectrum extends VisualizerBase implements IVisualizer
	{
		
		private var bytes: ByteArray;
		
		private var output: BitmapData;
		private var peaks: BitmapData;
		private var displace: Matrix;
		private var rect: Rectangle;
		private var gradient: Array;
		private var darken: ColorTransform;
		
		private var outputColor:Number = 0xff000000;
		
		private var bitmapOutput:Bitmap;
		private var bitmapPeaks:Bitmap;
		
		private var creditField:TextField;
		
		public function get visualizerName():String
		{
			return "AMS Spectrum";
		}
		
		public function get authorName():String
		{
			return "Andre Michelle";
		}
		
		public function AMSpectrum()
		{	
			super();
			
			bytes = new ByteArray();
		
			output = new BitmapData( 350, 350, true, 0 );
			peaks = new BitmapData( 350, 350, true, 0 );		
			
			displace = new Matrix();
			displace.tx = 2;
			displace.ty = -1;
			
			darken = new ColorTransform( 1, 1, 1, 1, -2, -2, -2, 0 );
			
			rect = new Rectangle( 0, 0, 1, 0 );

			bitmapOutput = new Bitmap(output);
			bitmapPeaks = new Bitmap(peaks);

			addChild(bitmapOutput);
			addChild(bitmapPeaks);
			
			gradient = createRainbowGradientArray();
			
			creditField = new TextField();	
			creditField.text = "AMSpectrum Visualizer by Andre Michelle";
			creditField.setTextFormat(new TextFormat("Arial",10, 0xFFFFFF));
			creditField.alpha = .5;
			creditField.autoSize = TextFieldAutoSize.RIGHT;
			creditField.selectable = false;
			creditField.y = 0;
			creditField.x = unscaledWidth - creditField.width;
			addChild(creditField);
		}
			
		protected override function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{			
			graphics.clear();
			graphics.beginFill( 0 , 1);
			graphics.drawRect( 0, 0, unscaledWidth, unscaledHeight );
			graphics.endFill();			
			
			output = new BitmapData( unscaledWidth, unscaledHeight, true, 0 );
			peaks = new BitmapData( unscaledWidth, unscaledHeight, true, 0 );		

			bitmapOutput = new Bitmap(output);
			bitmapPeaks = new Bitmap(peaks);

			addChild(bitmapOutput);
			addChild(bitmapPeaks);			
			
			creditField.x = unscaledWidth - creditField.width;
			
		}	
			
		protected override function draw(): void
		{						
			peaks.fillRect( peaks.rect, 0 );
			
			SoundMixer.computeSpectrum( bytes, true, 0 );
			
			var value: Number;
			var height: Number;
			
			var smooth: Number;
			
			for( var i: int = 0 ; i < 256 ; i++ )
			{
				value = bytes.readFloat();
				
				if( i == 0 ) smooth = value;
				else smooth += ( value - smooth ) / 8;
				
				height = 2 + smooth * 0xf0;
				
				rect.x = 8 + i;
				rect.y = 320 + ( i >> 2 ) - height;
				rect.height = height;
				
				peaks.setPixel32( rect.x, rect.y, 0xffffffff );

				output.fillRect( rect, outputColor | gradient[i] );
			}
			
			output.draw( output, displace, darken, null, null, true );
		}
		
		private function createRainbowGradientArray(): Array
		{
			var gradient: Array = new Array();

			var shape: Shape = new Shape();
			var bmp: BitmapData = new BitmapData( 256, 1, false, 0 );
			
			var colors: Array = [ 0, 0xff0000, 0xffff00, 0x00ff00, 0x00ffff ];
			var alphas: Array = [ 100, 100, 100, 100, 100 ];
			var ratios: Array = [ 0, 16, 128, 192, 255 ];
			
			var matrix: Matrix = new Matrix();
			
			matrix.createGradientBox( 256, 1, 0, 0, 0 );
			
			shape.graphics.beginGradientFill( 'linear', colors, alphas, ratios, matrix );
			shape.graphics.drawRect( 0, 0, 256, 1 );
			shape.graphics.endFill();
			
			bmp.draw( shape );
			
			for( var i: int = 0 ; i < 256 ; i++ )
			{
				gradient[i] = bmp.getPixel( i, 0 );
			}
			
			return gradient;
		}		
	}
}